package com.example.thymeleafrbac.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.thymeleafrbac.pojo.SysUser;
import com.example.thymeleafrbac.service.SysUserService;
import com.example.thymeleafrbac.util.MD5Util;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

@Controller
public class LoginController {

    @Resource
    SysUserService sysUserService;

    @GetMapping("login")
    public String hello(){
        return "login";
    }


    @RequestMapping(value="loginIn")
    public String login(String username, String password, Model model){
        String salt = "hello";//加入的盐
        String newPassword= MD5Util.backToDb( password,salt);
        QueryWrapper<SysUser> wrapper=new QueryWrapper<>();
        wrapper.eq("login_name",username);
        wrapper.eq("password",newPassword);
        SysUser one = sysUserService.getOne(wrapper, true);
        if (one!=null){
            model.addAttribute("user",one);
            return "index";
        }
        model.addAttribute("err","输入错误");
        return "login";
    }


}
